package com.though.logistics.handler;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.though.http.method.Post;
import com.though.http.soa.HttpInvoker;
import com.though.http.soa.HttpResponse;
import com.though.logistics.config.ConfigParams;
import com.though.logistics.config.LogisticsConfig;
import com.though.logistics.entity.ParamsRequest;
import com.though.logistics.entity.SubscrieResponse;
import com.though.logistics.entity.TaskRequest;
import com.though.logistics.entity.TaskResponse;
import com.though.logistics.helper.JacksonHelper;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;


public class Logistics {
	
	private static final Logger logger = LoggerFactory.getLogger(Logistics.class);
	
	private static ConfigParams configParams = LogisticsConfig.getConfigParams();
	
	
	/**
	 * 订阅物流信息
	 */
	public static boolean subscriptionLogistics(TaskRequest taskRequest) {
		HashMap<String, String> params = new HashMap<String, String>();
		ConfigParams configParams = LogisticsConfig.getConfigParams();
		taskRequest.setKey(configParams.getApikey());
		taskRequest.getParameters().put("callbackurl", configParams.getCallback());
		params.put("schema", "json");
		params.put("param", JacksonHelper.toJSON(taskRequest));
		try {
			Post post = new Post(configParams.getGatway());
			post.setBodyParams(params);
			HttpResponse response = HttpInvoker.execute(post);
			String result = response.getBody();
			TaskResponse res = JacksonHelper.fromJSON(result, TaskResponse.class);
			logger.info("订阅物流信息成功,物流单号为: "+ taskRequest.getNumber());
			return res.getResult();
		} catch (Exception e) {
			logger.error("订阅物流信息失败,物流单号为: " + taskRequest.getNumber());
			e.printStackTrace();
		}
		return false;
	}
	
	
	public static boolean subscrieLogistics(String callback, ParamsRequest request) {
		JSONObject json = processParams(callback, request);
		Post post = new Post(configParams.getGatway());
		post.setBodyParam("schema", "json");
		post.setBodyParam("param", JSON.toJSONString(json));
		
		String text = null;
		try {
			text = HttpInvoker.execute(post).getBody();
			if (StringUtils.isBlank(text) || !text.startsWith("{")) throw new RuntimeException("订阅物流，返回空数据或数据格式不对！");
			SubscrieResponse response = JSON.parseObject(text, SubscrieResponse.class);
			return response.getResult();
		} catch (Exception e) {
			logger.error("订阅物流信息发生异常，params: " + JSON.toJSONString(request), e);
		}
		return false;
	}


	private static JSONObject processParams(String callback, ParamsRequest request) {
		JSONObject jsonCallback = new JSONObject();
		jsonCallback.put("callbackurl", callback);
		JSONObject json = new JSONObject();
		json.put("company", request.getCompany());
		json.put("number", request.getNumber());
		json.put("from", request.getFrom());
		json.put("to", request.getTo());
		json.put("parameters", jsonCallback);
		
		json.put("key", configParams.getApikey());
		
		return json;
	}
	
	
	public static void main(String[] strs) {
		ParamsRequest request = new ParamsRequest();
		request.setCompany("shunfeng");
		request.setNumber("786918903801");
		request.setFrom("广东省广州市天河区");
		request.setTo("江西省新余市仙女湖区");
		subscrieLogistics("wwww.baidu.com", request);
	}
}
